package com.sasac.test.dao;

import com.sasac.test.domain.Student;
import com.sasac.test.projection.StudentProjection;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
public interface StudentDao extends PagingAndSortingRepository<Student, Long>, JpaSpecificationExecutor<Student>, JpaRepository<Student,Long> {

    Student findById(String id);

    @Query("SELECT s.name as studentName,sc.name as schoolName,s.id as id from Student s,School sc where s.id=sc.student_id and s.id = :id")
    StudentProjection findStudentInfo(@Param("id") Long id);


    @Query("SELECT s.name as studentName,sc.name as schoolName,s.id as id from Student s,School sc where s.id=sc.student_id and s.id = :id")
    Page<StudentProjection> findStudentInfo1(@Param("id") Long id, Pageable pageable);

    @Query("SELECT s.name as studentName,sc.name as schoolName,s.id as id from Student s,School sc where s.id=sc.student_id and s.id = :id")
    List<StudentProjection> findStudentInfo2(@Param("id") Long id);
}
